package top.eggcode.system.modules.user.store;

import org.springframework.stereotype.Repository;
import top.eggcode.common.base.JpaSimpleStore;
import top.eggcode.common.model.ListPage;
import top.eggcode.plugins.query.CriteriaBuilder;
import top.eggcode.system.modules.user.model.PowerEntity;
import top.eggcode.system.modules.user.model.PowerQuery;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

/**
 * Title:
 * Description: TODO
 * Date: 2021/9/19 9:59
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Repository
public class PowerStore extends JpaSimpleStore<PowerEntity, String> {

    @Resource
    private CriteriaBuilder builder;

    public PowerStore(EntityManager manager) {
        super(PowerEntity.class, manager);
    }

    public ListPage<PowerEntity> page(PowerQuery query) {
        return builder.createQuery(PowerEntity.class)
                .fromEntity("root")
                .where(condition -> {
                    if (query.getName() != null) {
                        condition.likeAny("root.name", query.getName());
                    }
                    if (query.getPath() != null) {
                        condition.likeAny("root.path", query.getPath());
                    }
                })
                .fetchPage(query);
    }
}
